$(function () {
    getArticle()
    let index = null
    $("#btnAdd").on('click', function () {
        index = layer.open({
            type: 1,
            title: '添加文章',
            content: $('#content').html(),
            area: ['500px', '250px']
        });
    })
    $("body").on('submit', '#formAdd', function (e) {
        e.preventDefault()
        console.log('1');
        $.ajax({
            type: "post",
            url: "/my/article/addcates",
            data: $("#formAdd").serialize(),
            success: function (res) {
                if (res.status != 0) return layui.layer.msg(res.message)
                layui.layer.msg(res.message)
                getArticle()
                layer.close(index)
            }
        });
    })
    let index1 = null
    $("body").on('click', '#edit', function () {
        // console.log('1');
        let id = $(this).attr('data-id')
        // console.log(Id);
        index1 = layer.open({
            type: 1,
            title: '修改文章',
            content: $('#contentEdit').html(),
            area: ['500px', '250px']
        });
        $.ajax({
            type: "get",
            url: "/my/article/cates/" + id,
            success: function (res) {
                if (res.status != 0) return layui.layer.msg(res.message)
                layui.layer.msg(res.message)
                layui.form.val('formEdit', res.data)
            }
        });
    })
    $("body").on('submit', '#formEdit', function (e) {
        e.preventDefault()
        console.log('1');
        $.ajax({
            type: "post",
            url: "/my/article/updatecate",
            data: $(this).serialize(),
            success: function (res) {
                if (res.status != 0) return layui.layer.msg(res.message)
                layui.layer.msg(res.message)
                getArticle()
                layer.close(index1)
            }
        });
    })
    $("body").on('click', '#del', function () {
        // console.log('1');
        let id = $(this).attr('data-id')
        console.log(id);
        layer.confirm('确认删除吗?', { icon: 3, title: '提示' }, function (index) {
            //do something
            // console.log('1');
            $.ajax({
                type: "get",
                url: "/my/article/deletecate/" + id,
                success: function (res) {
                    if (res.status != 0) return layui.layer.msg(res.message)
                    getArticle()
                    layer.msg('删除成功', {icon: 1}); 
                }
            });
            layer.close(index);
        });
    })
})
function getArticle() {
    $.ajax({
        type: "get",
        url: "/my/article/cates",
        success: function (res) {
            console.log(res);
            let htmlStr = template('tpl', res)
            // console.log(htmlStr);
            $("tbody").html(htmlStr)
        }
    });
}